﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration;

namespace DataModel
{
    public enum Sex { Unknown, Male, Female };

    public abstract class Person
    {        
        [Column("ID")]
        public int ID { get; set; }
        [Column("FIRSTNAME")]
        public string FirstName { get; set; }           
        [Column("LASTNAME")]
        public string LastName { get; set; }

        [Column("SEX")]
        public string SexAsString { get; set; } 
        [Column("BIRTHPLACE")]
        public string BirthPlace { get; set; }

        public Sex Sex
        {
            get
            {
                switch (SexAsString)
                {
                    case "W":
                    case "F":
                        return Sex.Female;
                    case "M":
                        return Sex.Male;
                    default:
                        return Sex.Unknown;
                };
            }
            set
            {
                switch (value)
                {
                    case Sex.Female:
                        SexAsString = "F";
                        break;
                    case Sex.Male:
                        SexAsString = "M";
                        break;
                    case Sex.Unknown:
                        SexAsString = "?";
                        break;
                }
            }
        }
        public virtual List<Phone> Phones { get; set; } // List with phone numbers
        public virtual Address ADDRESS { get; set; }

        public virtual List<BookCopy> BorrowedBooks { get; set; }
    }

    public class PersonConfiguration : EntityTypeConfiguration<Person>
    {
        public PersonConfiguration()
        {
            
            HasKey(x => x.ID);
            Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            HasMany( x => x.Phones )
                .WithMany( x => x.Persons )
                .Map( x => {
                    x.MapLeftKey("PERSON_ID");
                    x.MapRightKey("PHONE_ID");
                    x.ToTable("PERSON_PHONE");
                });
            HasOptional(x => x.ADDRESS);
            Property(x => x.LastName).IsRequired();
            ToTable("PERSON");
        }   
    }
}
